package com.golxen.framework.manager;


import com.golxen.framework.executor.GeneralTask;
import com.golxen.framework.executor.ThreadPlugin;
import com.golxen.utils.LogUtil;

import java.util.List;

public class MgrDataSaveTask extends GeneralTask {

    private static int saveMgrThreadCount = ThreadPlugin.getCpu();

    private int saveIndex;

    public MgrDataSaveTask(int saveIndex) {
        this.saveIndex = saveIndex;
    }

    @Override
    public void doRun() {
        List<TempMgr> mgrList = ManagerPlugin.getMgrs();
        for (int index = 0; index < mgrList.size(); index++) {
            if (mgrList.get(index) == null) {
                continue;
            }
            if (index % saveMgrThreadCount != saveIndex) {
                continue;
            }

            long begin = System.currentTimeMillis();
            TempMgr mgr = mgrList.get(index);

            try {
                mgr.save();
            } catch (Throwable t) {
                LogUtil.COMMON.error("", t);
            }
            long cost = System.currentTimeMillis() - begin;
            if (cost > 1000) {
                getLOGGER().info("save {}  cost {} ms", mgr.getClass().getSimpleName(), cost);
            }
        }
    }
}
